package com.lbd.bonus.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.bonus.bean.SysUserBonus;
import com.lbd.bonus.dao.UserBonusRepository;
import com.lbd.bonus.service.UserBonusService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by Yuanbo on 2016/9/22.
 * 关于用户奖励信息的业务层
 */
@Service
@Transactional
public class UserBonusServiceImpl extends BaseServiceImpl<SysUserBonus, UserBonusRepository> implements UserBonusService {
    @Autowired
    UserBonusRepository userBonusRepository;

    /**
     * 用户奖励信息查询业务层实现
     *
     * @param sysUserBonus 用户奖励信息
     * @param parameter    筛选条件
     * @return 查询用户奖励信息结果
     */
    @Override
    public String buildJpql(SysUserBonus sysUserBonus, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder(" from SysUserBonus t");
        if (sysUserBonus != null) {
            if (sysUserBonus.getState() != null) {
                jpqlBuilder.append(" and t.state = :state");
                parameter.put("state", sysUserBonus.getState());
            }
            if (StringUtils.isNotBlank(sysUserBonus.getUserId())) {
                jpqlBuilder.append(" and t.userId like :userId");
                parameter.put("userId", "%" + sysUserBonus.getUserId() + "%");
            }
        }
        jpqlBuilder.append(" order by modifiedDate desc ");
        return jpqlBuilder.toString();
    }

    @Override
    public SysUserBonus findSysUserBonusById(String id) {
        return userBonusRepository.findOne(id);
    }
}
